package com.txw.javaCore.server.util;

import com.txw.javaCore.server.workFilter.SensitiveFilter;
import org.apache.commons.lang3.StringUtils;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION: 敏感词自动过滤工具，应用场景：发微博、转发微博，评论，回复
 * @Author: 涂玄武
 * @DATE: 2021/2/25 11:12
 */
public class SensitiveWordUtil {

    private static final SensitiveFilter FILTER = SensitiveFilter.DEFAULT;

    private static final Character word = '*';

    /**
     * 判断是否含有敏感词
     *
     * @param sentence
     * @return
     */
    public static Boolean hasSenWord(final String sentence) {
        if (StringUtils.isBlank(sentence)) {
            return false;
        }
        //包含敏感词以*替换，不包含则保持原样
        String filterAfter = FILTER.filter(sentence, word);
        return !StringUtils.equals(sentence, filterAfter);
    }

    /**
     * 处理含有敏感词的句子
     *
     * @param sentence
     * @param word
     * @return
     */
    public static String manageSenWord(final String sentence, final Character word) {
        return FILTER.filter(sentence, word);
    }

//    public static void main(String[] args) {
//        String content = "我去，日了狗了！";
//        //方式一：替换敏感词为*
//        System.out.println(SensitiveWordUtil.manageSenWord(content,'*'));
//        //方式二：判断是否包含敏感词，有则提示报错
//        System.out.println(SensitiveWordUtil.hasSenWord(content));
//    }

}
